package org.liukai.tutorial.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.liukai.tutorial.domain.Role;
import org.liukai.tutorial.domain.User;
import org.liukai.tutorial.service.IAccountService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 用户
 * 
 * @author liukai
 * 
 */
@Controller
@RequestMapping("/account/user")
public class UserAction {

	protected static Logger logger = Logger.getLogger("controller");

	// 重定向至User列表
	private final String USER_LIST = "redirect:/account/user";

	@Resource(name = "accountService")
	private IAccountService service;

	/**
	 * 获得所有的Role列表返回到页面.
	 */
	@ModelAttribute("roleList")
	public List<Role> getAllRole() {
		return service.getAllRole();
	}

	/**
	 * 获得所有的User并传递至user page.
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String getAllUserPage(Model model) {

		model.addAttribute("userList", service.getAllUser());

		return "user";
	}

	/**
	 * 初始化一个User至addUser page
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.GET)
	public String getAddUser(Model model) {

		model.addAttribute("userAttribute", new User());

		return "addUser";
	}

	/**
	 * 新增User
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView AddUser(@ModelAttribute("userAttribute") User user) {
		service.addUser(user);
		return new ModelAndView(USER_LIST);
	}

	/**
	 * 根据ID删除User
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteUser(@PathVariable(value = "id") Integer id) {

		service.deleteUser(id);

		return new ModelAndView(USER_LIST);

	}

	/**
	 * 根据ID获得User传至editUser page
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String getEditUserById(@PathVariable(value = "id") Integer id,
			Model model) {

		User user = service.getUserById(id);

		Map<Integer, Integer> map = new HashMap<Integer, Integer>();

		// 将用户拥有的角色ID存入Map中
		for (int i = 0; i < user.getRoleIds().size(); i++) {
			map.put(user.getRoleIds().get(i), user.getRoleIds().get(i));
		}

		user.setRoleCheckBox(map);

		model.addAttribute("userAttribute", user);

		return "editUser";
	}

	/**
	 * 更新修改后的User
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public ModelAndView editUser(@ModelAttribute("userAttribute") User user,
			@PathVariable(value = "id") Integer id) {
		user.setId(id);
		service.updateUser(user);
		return new ModelAndView(USER_LIST);

	}

	/**
	 * 远程验证loginName是否唯一.
	 */
	@RequestMapping(value = "/checkLoginName", method = RequestMethod.GET)
	public @ResponseBody
	boolean checkLoginName(@RequestParam(value = "loginName") String loginName,
			@RequestParam(value = "oldLoginName") String oldLoginName) {
		
		return service.isLoginNameUnique(loginName, oldLoginName);
	}

}
